#include<iostream>
#include<signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

int cnt=3;
void handler(int signo)
{
    cout<<"get a signo:"<<signo<<endl;
    pid_t id;
    while((id=waitpid(-1,nullptr,WNOHANG))>0)
    {
        cout<<"get child_pid:"<<id<<endl;
    }
}
int main()
{
    signal(SIGCHLD,handler);
    //signal(SIGCHLD,SIG_IGN);//系统回收子进程
    pid_t id = fork();
    if(id==0)
    {
        while(cnt)
        {
            cout<<"cnt = "<<cnt<<endl;
            sleep(1);
            cnt--;
        }
        exit(1);
    }
    while(1)
    {
        cout<<"cnt = "<<cnt<<endl;
        sleep(1);
    }

    return 0;
}